/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.jsfbeans;

import jard.webshop.management.ProductManagement;
import jard.webshop.nbp.Product;
import jard.webshop.util.ProductException;
import jard.webshop.util.Utils;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author CJP
 */
@ManagedBean(name = "stockBackingBean")
@SessionScoped // Passing parameters around isn't pretty either
public class StockBackingBean implements Serializable {
    
    private Product product = null;
    private Integer addition = 0;
    private Integer newCount = 0;

    /** Creates a new instance of StockBackingBean */
    public StockBackingBean() {
        System.out.println("Constructor of StockBackingBean");
    }
    
    public String add(Product product) {
        System.out.println("add in stockBackingBean");
        this.product = product;
        return "addstock";
    }
    
    public String count(Product product) {
        System.out.println("count in stockBackingBean");
        this.product = product;
        return "countstock";
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Integer getAddition() {
        return addition;
    }

    public void setAddition(Integer addition) {
        this.addition = addition;
    }

    public Integer getNewCount() {
        return newCount;
    }

    public void setNewCount(Integer newCount) {
        this.newCount = newCount;
    }
    
    public Integer getStock() {
        return product.getStock();
    }
    
    public String saveCount() {
        System.out.println("saveCount in stockBackingBean");
        
        System.out.println("product => " + product);
        System.out.println("newCount  => " + newCount);
        ProductManagement pm = ProductManagement.getInstance();
        try {
            pm.setStock(product,newCount);
            product = pm.getProduct(product);
        } catch (ProductException ex) {
            System.out.println("Gonna growl: " + ex.getMessage());
            Utils.growl("Failure", ex.getMessage());
            return "";
        }

        return "stock";
    }
    
    public String saveAddition() {
        System.out.println("saveAddition in stockBackingBean");
        
        System.out.println("product => " + product);
        System.out.println("addition => " + addition);
        ProductManagement pm = ProductManagement.getInstance();
        try {
            pm.addStock(product, addition);
            product = pm.getProduct(product);
        } catch (ProductException ex) {
            System.out.println("Gonna growl: " + ex.getMessage());
            Utils.growl("Failure", ex.getMessage());
            return "";
        }

        return "stock";
    }
}
